package com.scope.imageFilters;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

/**
 * Created by Samrith on 12/10/13.
 */
public final class FilterManager {
    public FilterManager(Context context, String filterName, String paramStr) {
        mContext = context;
        mFilterName = filterName;
        mParamStr = paramStr;
        mKnownFilter = false;
        instanciate();
    }

    private Context mContext;

    private Boolean mKnownFilter;
    public Boolean isKnownFilter() {
        return mKnownFilter;
    }

    private String mFilterName;
    public String getFilterName() {
        return mFilterName;
    }

    private BaseFilter mFilter;
    public BaseFilter getFilter() {
        return mFilter;
    }

    private String mParamStr;
    private Object mParam;
    public Object getParam() {
        return mParam;
    }

    private void instanciate() {
        if (mFilterName == Adaptive.getFilterName()) {
            mFilter = new Adaptive(mContext);
            mParam = AdaptiveType.fromString(mParamStr);
        } else if (mFilterName == Brightness.getFilterName()) {
            mFilter = new Brightness(mContext);
            mParam = Integer.valueOf(mParamStr);
        } else if (mFilterName == Contrast.getFilterName()) {
            mFilter = new Contrast(mContext);
            mParam = Integer.valueOf(mParamStr);
        } else if (mFilterName == Histogram.getFilterName()) {
            mFilter = new Histogram(mContext);
            mParam = null;
        } else if (mFilterName == GrayScale.getFilterName()) {
        	mFilter = new GrayScale(mContext);
        	mParam = null;
        } else if (mFilterName == EdgeDetection.getFilterName()) {
        	mFilter = new EdgeDetection(mContext);
        	mParam = null;
        } else if (mFilterName == Morphing.getFilterName()) {
            mFilter = new Morphing(mContext);
            mParam = MorphoParam.fromString(mParamStr);
        } else if (mFilterName == Threshold.getFilterName()) {
        	mFilter = new Threshold(mContext);
        	mParam = ThreshParam.fromString(mParamStr);
        } else {
            throw new IllegalArgumentException(mFilterName + ": " + mParamStr);
        }

        mFilter.setParam(mParam);
    }

    public String applyFilter(String imagePath) {
        mFilter.setSourcePath(imagePath);
        mFilter.process();
        return mFilter.getDestPath();
    }

    public Bitmap applyFilter(Bitmap image) {
        mFilter.setSourceImage(image);
        mFilter.setParam(mParam);
        Log.v("SCOPE 2", "manager appling filter...");
        mFilter.process();
        Log.v("SCOPE 2", "manager applied filter...");
        return mFilter.getFilteredImage();
    }
}
